﻿using System;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using XFSale.Lib;

namespace XFSale.UI
{
    public partial class WinLogin : WinBase
    {
        public WinLogin()
        {
            InitializeComponent();
        }

        private void WinPasswod_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (DialogResult == DialogResult.Retry)
            {
                e.Cancel = true;
            }
        }

        private void HandleResponse(Pack response)
        {
            if (response.GetBaseByName("ReturnCode") == "0000")
            {
                App.Global.RegionCode = response.GetRecordByName("RegionCode");
                App.Global.BranchCode = response.GetRecordByName("BranchCode");
                App.Global.OpID = response.GetRecordByName("OpID");
                App.Global.OpName = response.GetRecordByName("OpName");
                App.Global.SysStatus = response.GetRecordByName("SysStatus");
                App.Global.SysWorkDay = response.GetRecordByName("SysWorkDay");
                App.Global.SysVersion = response.GetRecordByName("SysVersion");
                DialogResult = DialogResult.OK;
                return;
            }
            else
            {
                MessageBox.Show(response.GetBaseByName("ReturnMsg"));
                DialogResult = DialogResult.Retry;
            }
        }

        private void BtnOk_Click(object sender, EventArgs e)
        {
            string channelCode = System.Configuration.ConfigurationSettings.AppSettings["ChannelCode"];
            string regionCode = System.Configuration.ConfigurationSettings.AppSettings["RegionCode"];
            string branchCode = System.Configuration.ConfigurationSettings.AppSettings["BranchCode"];
            string opID = TxtOpID.Text;
            string opPassword = TxtPassword.Text;

            if (!Regex.IsMatch(channelCode, @"^\d{3}$"))
            {
                SysMessage.Error("渠道编号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(regionCode, @"^\d{4}$"))
            {
                SysMessage.Error("地区编号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(branchCode, @"^\d{8}$"))
            {
                SysMessage.Error("网点号码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(opID, @"^\d{1,8}$"))
            {
                SysMessage.Error("用户帐号不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            if (!Regex.IsMatch(opPassword, @"^\d{6}$"))
            {
                SysMessage.Error("用户密码不合法");
                DialogResult = DialogResult.Retry;
                return;
            }

            try
            {
                //初始化请求包
                Log.LogDebug("初始化请求包");
                request.ServiceCode = "111201";
                request.PackType = "R";
                request.Init();

                //填充请求包基础数据
                Log.LogDebug("填充请求包基础数据");
                request.SetBaseByName("ServiceCode", "111201");
                request.SetBaseByName("ChannelCode", channelCode);
                request.SetBaseByName("RegionCode", regionCode);
                request.SetBaseByName("BranchCode", branchCode);
                request.SetBaseByName("AcceptMethod", "0");
                request.SetBaseByName("OpID", opID);

                //填充请求包记录数据
                Log.LogDebug("填充请求包记录数据");
                request.SetRecordByName("OpPassword", opPassword);

                //调用服务
                Log.LogInfo("开始调用服务");
                App.CallService("O111201", HandleResponse);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                DialogResult = DialogResult.Retry;
            }
            finally
            {
                //释放请求包
                Log.LogDebug("释放请求包");
                request.Dispose();
            }
        }
    }
}
